require(haven)
require(ggplot2)
require(scales)

itanes_2013 <- 
  read_spss("ITA2013_(envers2015_01_19).sav") # Not included

itanes_2013$m5s <- itanes_2013$d89==6 
itanes_2013$m5s <- 
  as.factor(ifelse(itanes_2013$m5s==TRUE, 
                   'M5S voters', 'others'))

require(plyr)
itanes_2013$d4_recoded <- 
  revalue(as.factor(itanes_2013$d4),
          c('-1' = 'Working', 
            '1' = 'Retired',
            "2" = "Housewife",
            "3" = "Student",
            "5" = "Unemployed",
            "6" = "Unemployed",
            "8" = "Unemployed",
            "9" = "Unemployed",
            "10" = NA,
            "12" = NA))
itanes_2013$stu_recoded <-
  revalue(as.factor(itanes_2013$stu),
          c('1' = '1', 
            "2" = "2",
            "3" = "3",
            "4" = "4",
            "5" = "5",
            "6" = "5",
            "7" = "5",
            "8" = "5",
            "9" = "6",
            "10" = "6",
            "11" = NA))

itanes_2013$lav3_recoded <-
  revalue(as.factor(itanes_2013$lav3), 
          c('1' = 'Yes, frequently',
            '2' = 'Yes, sometimes',
            '3' = 'No',
            '4' = NA))

df_occupation <- 
  as.data.frame(prop.table(table(itanes_2013$m5s, itanes_2013$d4_recoded), 1))

# Family wealth
prop.table(table(itanes_2013$m5s, itanes_2013$d7_5), 1)

chisq.test(with(subset(itanes_2013,  d7_1!=3), table(m5s, d7_1)))
chisq.test(with(subset(itanes_2013,  d7_2!=3), table(m5s, d7_2)))

family_wealth_m <-
    rbind(prop.table(with(subset(itanes_2013,  d7_1!=3), table(m5s, d7_1)), 1),
          prop.table(with(subset(itanes_2013,  d7_2!=3), table(m5s, d7_2)), 1),
          prop.table(with(subset(itanes_2013,  d7_3!=3), table(m5s, d7_3)), 1))
          # prop.table(with(subset(itanes_2013,  d7_4!=3), table(m5s, d7_4)), 1),
          # prop.table(with(subset(itanes_2013,  d7_5!=3), table(m5s, d7_5)), 1),
          # prop.table(with(subset(itanes_2013,  d7_6!=3), table(m5s, d7_6)), 1))
family_wealth_m <- cbind(family_wealth_m, rownames(family_wealth_m))
rownames(family_wealth_m) <- NULL
family_wealth_df <- data.frame(family_wealth_m, stringsAsFactors = F)
family_wealth_df[[2]] <- NULL
names(family_wealth_df) <- c("yes", "vote")
family_wealth_df$yes <- as.numeric(as.character(family_wealth_df$yes))
family_wealth_df$question <- c('primary house', 'primary house',
                               'secondary house', 'secondary house',
                               'stocks', 'stocks')
                               # 'government bond', 'government bond',
                               # 'investments in real estate', 'investments in real estate',
                               # 'a business', 'a business')
family_wealth_df$question <- factor(family_wealth_df$question, levels = c('primary house',
                                                                          'secondary house',
                                                                          'stocks'))
p_wealth <- 
  ggplot(family_wealth_df, 
         aes(x=question, y=yes, fill=vote)) +
  geom_bar(position = "dodge", stat="identity") +
  labs(fill=NULL, y=NULL, x='Family ownership') +
  scale_y_continuous(label = scales::percent) +
  guides(fill=FALSE)

p1 <- 
  ggplot(df_occupation, aes(x=Var2, y=Freq)) +
  geom_bar(aes(fill = Var1), position = "dodge", stat="identity") +
  labs(x = 'Occupation', y=NULL, fill=NULL) +
  scale_y_continuous(labels = scales::percent) +
  guides(fill=FALSE)

df_stu <- 
  as.data.frame(prop.table(table(itanes_2013$m5s, itanes_2013$stu_recoded), 1))
itanes_2013$university_bin <-
  itanes_2013$stu_recoded == 6
# chisq.test(table(itanes_2013$m5s,
#       itanes_2013$university_bin))
# prop.table(table(itanes_2013$m5s,
#                  itanes_2013$university_bin), 1)
 
p_edu <- 
  ggplot(df_stu, aes(y=Freq, x=Var2, fill=Var1)) +
  geom_bar(stat = 'identity', position = 'dodge') +
  scale_y_continuous(label = scales::percent) +
  labs(x='Level of education', y=NULL, fill=NULL) +
  annotate('text', label = 'University\nlevel', x=6, y=0.3, size=3) +
  geom_segment(aes(x=6, y=0.25, xend=6, yend=0.2), arrow = arrow(length = unit(0.2, "cm")), size = 0.4)

# ggplot(df_stu, aes(x=Var2, y=Freq)) +
#   geom_bar(aes(fill = Var1), position = "dodge", stat="identity") +
#   labs(x=NULL, y=NULL, fill=NULL) +
#   scale_y_continuous(labels = scales::percent)

df_ecoworries <- 
  as.data.frame(prop.table(table(itanes_2013$m5s, itanes_2013$lav3_recoded), 1))

p2 <- 
  ggplot(df_ecoworries, aes(x=Var2, y=Freq)) +
  geom_bar(aes(fill = Var1), position = "dodge", stat="identity") +
  labs(x='Experience of economic hardship', y=NULL, fill=NULL) +
  scale_y_continuous(labels = scales::percent)


itanes_2013$d1_recoded <-
  as.numeric(as.character(itanes_2013$d1))
itanes_2013$d1_recoded[itanes_2013$d1_recoded  == -1] <- NA    

p3 <- 
  ggplot(itanes_2013, aes(x=m5s, y=d1_recoded, fill=m5s))+
  geom_boxplot()+
  labs(x='Age', y='years') +
  guides(fill = FALSE) +
  theme(# axis.title.x=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank())

# glm(m5s ~ d1_recoded, data = itanes_2013, family = 'binomial')

require(gridExtra)
grid.arrange(p3, p_edu, nrow = 1, widths=c(2,3))
grid.arrange(p_wealth, p1, p2, ncol = 3, widths=c(1,1,1.2))
 
